/*
 *   Author:         Kirin Zhong
 *   Created Date:   2024/11/25
 *
 *   Updated Date    Updated By      Reason of Update
 *   ------------    -----------     -----------------------------------------------
 */

import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.tess4j.*;
import net.sourceforge.tess4j.util.Utils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Slf4j
public class OcrTI {

    public static void main(String[] args) throws Exception {

        ITesseract instance = new Tesseract();
        instance.setDatapath("src\\main\\resources\\tess4j\\");
        instance.setLanguage("chi_sim+eng");

        File imageFile = new File("src\\main\\resources\\tess4j\\split\\33\\二值化\\");
//        System.out.println(imageFile.exists() + "--" + imageFile.getAbsolutePath());
        FileUtil.loopFiles(imageFile).forEach(file -> {
            try {
                doOcr(file, instance);
            } catch (TesseractException | IOException e) {
                throw new RuntimeException(e);
            }
        });
    }

    private static void doOcr(File imageFile, ITesseract instance) throws TesseractException, IOException {
        System.out.println(imageFile.getName() + " : " + instance.doOCR(imageFile));

        //按照每个字取词
//        int pageIteratorLevel = ITessAPI.TessPageIteratorLevel.RIL_SYMBOL;
//        log.info("PageIteratorLevel: " + Utils.getConstantName(pageIteratorLevel, ITessAPI.TessPageIteratorLevel.class));
//        BufferedImage bi = ImageIO.read(imageFile);
//        List<Word> result = instance.getWords(bi, pageIteratorLevel);
//
//        //print the complete result
//        for (Word word : result) {
//            word.getBoundingBox();
//            log.info(word.toString());
//        }
    }
}
